#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

int main()
{
  pid_t id = fork();
  if(id == 0)
  {
    //child
    int count = 5;
    while(count)
    {
      cout << "I am child: " << getpid() << " count: " << count << endl;
      count--;
      sleep(1);
    }
    int a = 10;
    int b = 0;
    a /= b;
    exit(0);
  }
  //parent
  int status = 0;
  pid_t ret = waitpid(id, &status, 0);
  if(ret == id)
  {
    cout << "wait success" << endl;
    cout << "exit code: " << ((status>>8)&0xFF) << endl;
    cout << "exit signal: " <<  (status&0x7F) << endl;
    cout << "core dump: " << ((status>>7)&0x1) << endl;
  }
  return 0;
}
